package com.example.reflect.springannotation.config;

import com.google.common.base.Throwables;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;

@Slf4j
public abstract class CommonSchedulerBase<T,S,O> {

    public T context;

    /**
     * 这个是用于重新定义返回值类型
     * @param modelClassPathName
     * @return
     */
    public CommonSchedulerBase initModel(String modelClassPathName){
        this.context = this.buildModel(modelClassPathName);
        if (ObjectUtils.isEmpty(context)) {
            throw new RuntimeException("");
        }
        return this;
    }

    private T buildModel(String modelClassPathName) {
        try {
            Class<?> clazz = Class.forName(modelClassPathName);
            return (T) clazz.newInstance();
        }catch (Exception e){
            log.error("构建对象_根据完整路径获取对象失败,失败原因:{}", Throwables.getStackTraceAsString(e));
            throw new RuntimeException("构建对象失败！");
        }
    }

    public T execute(O o,S s){
        return this.context;
    }
}
